﻿using FAMailUtils.Data.DAO;
using FAMailUtils.Data.DTO;
using FAMailUtils.Data.DAO;
using FAMailUtils.Data.DTO;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Script.Services;
using System.Web.Services;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace OnlineBusinesses.Areas.CustomerAdmin
{
    public partial class Rules : System.Web.UI.Page
    {
        DepartmentDAO dao = new DepartmentDAO();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                int did = 0; int aid = 0;
                int.TryParse(Request.QueryString["did"], out did);
                int.TryParse(Request.QueryString["aid"], out aid);
                if (did > 0)
                {

                    DepartmentDTO dto = dao.GetByDepartmentID(did, ((AdminCustomer_Master)Page.Master).getUserLogin().Client.UserID);
                    lblObject. Text = "Phòng ban: "+dto.DepartmentName;
                }
                else 
                {
                    FAMailUtils.Data.DAO.AccountDAO adao = new FAMailUtils.Data.DAO.AccountDAO();
                    FAMailUtils.Data.DTO.AccountDTO dto = adao.GetByAccountID(aid, ((AdminCustomer_Master)Page.Master).getUserLogin().Client.ClientID);
                    lblObject.Text = "Tài khoản: " + dto.AccountName;
                }
            }
        }
        public static UserLoginDTO getUserLogin()
        {
            return (UserLoginDTO)HttpContext.Current.Session["UserLogin"];
        }
        [WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public static List<ObjectFunctionDTO> LoadFunction(int parentid, int departmentid, int accountid)
        {
            UserLoginDTO userLogin = getUserLogin();
            List<ObjectFunctionDTO> result = new List<ObjectFunctionDTO>();
            if (userLogin != null)
            {
                DataTable table = new AccountDAO().LoadDepartmentFunctionByParentID(parentid, departmentid, userLogin.Client.UserID);
                if (accountid > 0 && departmentid == 0)
                {
                    table = new AccountDAO().LoadAccountFunctionByParentID(parentid, accountid, userLogin.Client.UserID);
                }
                foreach (DataRow r in table.Rows)
                {
                    ObjectFunctionDTO dto = new ObjectFunctionDTO();
                    dto.FunctionID = Convert.ToInt32(r["FunctionID"]);
                    dto.ParentID = Convert.ToInt32(r["ParentID"]);
                    dto.HasChild = Convert.ToInt32(r["HasChild"]);
                    dto.IsPermit = Convert.ToBoolean(r["IsPermit"]);
                    dto.FunctionName = r["FunctionName"] + "";
                    result.Add(dto);
                }
            }
            return result;
        }
        [WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public static void Confirm(int departmentid, int accountid, string ids)
        {
            try
            {
                UserLoginDTO userLogin = getUserLogin();
                if (userLogin != null)
                {
                    if (departmentid > 0)
                        new AccountDAO().SaveFunctionByDepartmentID(ids, departmentid, userLogin.Client.UserID);
                    else new AccountDAO().SaveFunctionByAccountID(ids, accountid, userLogin.Client.UserID);
                }
                else throw new Exception("Session Expire");
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}